//登录验证
function user_login() {
    var user_name = $("#user_name").val();
    if (user_name == null || user_name == "") {
        alert("账号不能为空");
        return;
    }
    var user_password = $("#user_password").val();
    if (user_password == null || user_password == "") {
        alert("密码不能为空");
        return;
    }
    $.post("/userLogin", $("form[name='user_from']").serialize(), function (data) {
        if (data == "OK") {
            window.location.href = "/hotel/index";
        } else if (data == "ADMIN") {
            window.location.href = "/admin/room/index";
        } else if (data == "ERROR") {
            alert("账户错误，请重新输入!");
        } else {
            alert("密码错误，请重新输入!");
        }
    })
}

//获得注册电子邮件
function getCode() {
    var user_email = $("#user_email").val();
    if (user_email == null || user_email == "") {
        alert("请输入邮箱，再获得验证码。");
        return;
    }
    $.get("/verification?user_email=" + user_email, function (data) {
        if (data == "OK") {
            alert("邮件已发送，请注意查收。");
        } else if (data == "ERROR") {
            alert("邮箱已存在，请换一个邮箱注册。");
        } else {
            alert("服务器错误，请稍后再试");
        }
    })
}

//注册
function user_register() {
    var user_name = $("#user_name").val();
    if (user_name == null || user_name == "") {
        alert("账号不能为空");
        return;
    }
    var user_password = $("#user_password").val();
    if (user_password == null || user_password == "") {
        alert("密码不能为空");
        return;
    }
    var user_identity_card = $("#user_identity_card").val();
    if (user_identity_card == null || user_identity_card == "") {
        alert("身份证不能为空");
        return;
    }
    //校验身份证
    if(!(/^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/.test(user_identity_card))){
        alert("身份证号有误，请重新输入");
        return false;
    }
    var user_truename = $("#user_truename").val();
    if (user_truename == null || user_truename == "") {
        alert("姓名不能为空");
        return;
    }
    var user_sex = $("#user_sex").val();
    if (user_sex == null || user_sex == "") {
        alert("性别不能为空");
        return;
    }
    if(user_sex!="男" && user_sex!="女"){
        alert("性别请输入 “男” 或者 “女”");
        return;
    }
    var user_phone = $("#user_phone").val();
    if (user_phone == null || user_phone == "") {
        alert("联系电话不能为空");
        return;
    }
    // 校验手机号
    if(!(/^1(3|4|5|7|8)\d{9}$/.test(user_phone))){
        alert("手机号码有误，请重新输入");
        return false;
    }
    var user_email = $("#user_email").val();
    if (user_email == null || user_email == "") {
        alert("邮箱不能为空");
        return;
    }
    var code = $("#code").val();
    if (code == null || code == "") {
        alert("验证码不能为空");
        return;
    }
    $.post("/useRegister", $("form[name='user_form']").serialize(), function (data) {
        if (data == "OK") {
            alert("注册成功！");
            window.location.href = "/login";
        } else if (data == "ERROR") {
            alert("注册失败，账号已被使用，请换一个账号!");
        } else if (data == "FAIL") {
            alert("验证码错误，请重新获取!");
        } else if (data == "CARD") {
            alert("注册失败，身份证已有账号。");
        } else {
            alert("验证码未获取，请先获取邮箱验证码！");
        }
    })
}

//找回密码页面
function getForgetPassword() {
    window.location.href = "/forget";
}

//获得电子邮件验证码（忘记密码）
function getCodeForget() {
    var user_name = $("#user_name").val();
    var user_email = $("#user_email").val();
    if (user_email == null || user_email == "") {
        alert("请输入邮箱，再获得验证码。");
        return 0;
    }
    $.get("/verificationForget?user_email=" + user_email+"&user_name="+user_name, function (data) {
        if (data == "OK") {
            alert("邮件已发送，请注意查收。");
        } else if(data == "ERROR"){
            alert("邮箱不存在。");
        }else {
            alert("邮箱和账户不匹配。");
        }
    })
}

//找回密码确认
function user_forget() {
    var user_name = $("#user_name").val();
    if (user_name == null || user_name == "") {
        alert("账号不能为空");
        return 0;
    }
    var user_email = $("#user_email").val();
    if (user_email == null || user_email == "") {
        alert("邮箱不能为空");
        return 0;
    }
    var code = $("#code").val();
    if (code == null || code == "") {
        alert("验证码不能为空");
        return 0;
    }
    var user_password = $("#user_password").val();
    if (user_password == null || user_password == "") {
        alert("新密码不能为空");
        return 0;
    }
    $.post("/userLoginForget", $("form[name='user_from']").serialize(), function (data) {
        if (data == "OK") {
            alert("密码修改成功，新密码是："+user_password);
            window.location.href = "/hotel/index";
        } else if (data == "ERROR") {
            alert("验证码错误!");
        } else if (data == "FAIL") {
            alert("邮箱账户不匹配!");
        } else {
            alert("请先获取验证码!");
        }
    })
}

//收藏
function roomCollect(room_id) {
    var id = room_id.getAttribute("data-thing");
    $.get("/room/roomCollect?id=" + id, function (data) {
        if (data == "OK") {
            location.reload();
        }
    })
}

//取消收藏
function roomCollectOver(room_id) {
    var id = room_id.getAttribute("data-thing");
    $.get("/room/roomCollectOver?id=" + id, function (data) {
        if (data == "OK") {
            location.reload();
        }
    })
}

//预定房间
function roomReserve(room_id) {
    var id = room_id.getAttribute("data-thing");
    var count = room_id.getAttribute("data-things");
    if(count == '0'){
        alert("房间不足");
        return;
    }
    $.get("/room/roomReserve?id=" + id, function (data) {
        if (data == "OK") {
            alert("预定成功，请进入“预定详情”进行查看");
            location.reload();
        }else {
            alert("已经预定过了，请在“预定详情”查看");
            location.reload();
        }
    })
}

//取消预定
function removeSchedule(id) {
    var schedule_id = id.getAttribute("data-thing");
    $.get("/room/removeSchedule?schedule_id=" + schedule_id, function (data) {
        if (data == "OK") {
            location.reload();
        }
    })
}

//进入付款页面
function addRoomOrder(id) {
    var schedule_id = id.getAttribute("data-thing");
    var count = id.getAttribute("data-things");
    if(count == '0'){
        alert("房间不足");
        return;
    }
    window.location.href = "/room/addRoomOrder?schedule_id="+schedule_id;
}

//判断时间
function getOrderDate() {
    var orders_start_date = $("#orders_start_date").val();
    if(orders_start_date == null || orders_start_date == ""){
        alert("开始时间不能为空");
        return 0;
    }
    var orders_end_date = $("#orders_end_date").val();
    if(orders_end_date == null || orders_end_date == ""){
        alert("结束时间不能为空");
        return 0;
    }
    var num = $("#num").val();
    if(!(/(^[1-9]\d*$)/.test(num))){
        alert("房间数量应该是正整数");
        return 0;
    }
    var room_amount = $("#room_amount").val();
    if(parseInt(num) > parseInt(room_amount)){
        alert("房间数量不足");
        return 0;
    }
    if(orders_end_date<=orders_start_date){
        alert("结束时间必须大于开始时间");
        $("#orders_end_date").val("");
        return 0;
    }
    var now = new Date();
    var year = padleft0(now.getFullYear());       //年
    var month = padleft0(now.getMonth() + 1);     //月
    var day = padleft0(now.getDate());
    var newDate = year + "-" + month + "-" + day;
    if(newDate > orders_start_date){
        alert("开始时间不能设置已过时间");
        $("#orders_start_date").val("");
        return 0;
    }
    return 1;
}

//查看需要付款的总价钱
function getOrderMoney() {
    var over = getOrderDate();
    if(over == '0'){
        return 0;
    }
    var num = $("#num").val();
    var orders_start_date = $("#orders_start_date").val();
    var orders_end_date = $("#orders_end_date").val();
    var date1=new Date(orders_start_date);
    var date2=new Date(orders_end_date);
    var date=(date2.getTime()-date1.getTime())/(1000*60*60*24);/*不用考虑闰年否*/
    var moneyOneDay = $("#moneyOneDay").val();
    $("#money").val("总金额：$"+date*moneyOneDay*parseInt(num)+"元");
    $("#orders_money").val(date*moneyOneDay*parseInt(num));
    $("#orders_date").val(date);
}
//补齐两位数
function padleft0(obj) {
    return obj.toString().replace(/^[0-9]{1}$/, "0" + obj);
}

//付款
function increaseOrder() {
    var over = getOrderDate();
    if(over == '0'){
        return;
    }
    getOrderMoney();
    $.post("/order/addOrder", $("form[name='order_from']").serialize(), function (data) {
        if(data == "OK"){
            alert("订单已经产生，1小时内没有付款将自动取消");
            window.location.href = "/order/pay";
        }else {
            alert("服务器内部错误");
        }
    })
}

//修改个人信息
function userUpdate() {
    var user_name = $("#user_name").val();
    if (user_name == null || user_name == "") {
        alert("账号不能为空");
        return false;
    }
    var user_identity_card = $("#user_identity_card").val();
    if (user_identity_card == null || user_identity_card == "") {
        alert("身份证不能为空");
        return false;
    }
    //校验身份证
    if(!(/^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/.test(user_identity_card))){
        alert("身份证号有误，请重新输入");
        return false;
    }
    var user_password = $("#user_password").val();
    if (user_password == null || user_password == "") {
        alert("密码不能为空");
        return false;
    }
    var user_truename = $("#user_truename").val();
    if (user_truename == null || user_truename == "") {
        alert("姓名不能为空");
        return false;
    }
    var user_sex = $("#user_sex").val();
    if (user_sex == null || user_sex == "") {
        alert("性别不能为空");
        return false;
    }
    if(user_sex!="男" && user_sex!="女"){
        alert("性别请输入 “男” 或者 “女”");
        return false;
    }
    var user_phone = $("#user_phone").val();
    if (user_phone == null || user_phone == "") {
        alert("联系电话不能为空");
        return false;
    }
    // 校验手机号
    if(!(/^1(3|4|5|7|8)\d{9}$/.test(user_phone))){
        alert("手机号码有误，请重新输入");
        return false;
    }
    var user_email = $("#user_email").val();
    if (user_email == null || user_email == "") {
        alert("邮箱不能为空");
        return false;
    }
    $.post("/user/userUpdate", $("form[name='userModify']").serialize(), function (data) {
        if (data == "OK") {
            alert("修改成功！");
            window.location.href = "/login";
        } else if (data == "ERROR") {
            alert("修改失败，账号已被使用，请换一个账号!");
        } else if (data == "FAIL") {
            alert("修改失败，该邮箱已存在!");
        } else if (data == "CARD") {
            alert("修改失败，身份证号码已存在。");
        } else {
            alert("验证码未获取，请先获取邮箱验证码！");
        }
    })
}

//从订单付款
function payOrder(id) {
    var orders_id = id.getAttribute("data-thing");
    var orders_money = id.getAttribute("data-things");
    window.location.href = "/order/pay?orders_id="+orders_id+"&orders_money="+orders_money;
}

//进入评论页面
function modifyScheduleComment(id) {
    var schedule_id = id.getAttribute("data-thing");
    window.location.href = "/room/modifyScheduleComment?schedule_id="+schedule_id;
}

//评论
function addComment() {
    $.post("/room/addComment", $("form[name='schedule_from']").serialize(), function (data) {
        if (data == "OK") {
            alert("评论成功！");
            window.location.href = "/order/orderList1";
        } else {
            alert("评论失败！");
            window.onload;
        }
    })
}


//--------------------------------------------------管理员-------------------------------------


//备份数据库数据
function reportFormError() {
    var a = confirm("确认备份？");
    if(!a){
        return 0;
    }
    $.get("/report/reportFormError", function (data) {
        if (data == "OK") {
            alert("备份成功，备份数据在电脑C盘sql文件夹下");
        }else {
            alert("备份失败");
        }
    })
}

//导出订单报表
function reportFormOrder() {
    window.location.href = "/report/reportFormOrder";
}

//导出房间报表
function reportFormRoom() {
    window.location.href = "/report/reportFormRoom";
}

//导出用户报表
function reportFormUser() {
    window.location.href = "/report/reportFormUser";
}

//设置评论发布状态
function reduceScheduleComment(id) {
    var schedule_id = id.getAttribute("data-thing");
    var state = id.getAttribute("data-things");
    var a = confirm("确认此操作？");
    if(!a){
        return 0;
    }
    $.get("/admin/room/reduceScheduleComment?schedule_id=" + schedule_id + "&state=" + state, function (data) {
        if (data == "OK") {
            location.reload();
        }
    })
}

//管理员删除订单
function removeOrders(id) {
    var orders_id = id.getAttribute("data-thing");
    var a = confirm("确认删除吗？");
    if(!a){
        return 0;
    }
    $.get("/admin/room/removeOrders?orders_id=" + orders_id, function (data) {
        if (data == "OK") {
            location.reload();
        }
    })
}

//删除房间信息
function roomDelete(id) {
    var room_id = id.getAttribute("data-thing");
    var a = confirm("确认删除吗？");
    if(!a){
        return 0;
    }
    $.get("/admin/room/roomDelete?room_id=" + room_id, function (data) {
        if (data == "OK") {
            location.reload();
        }
    })
}